//
//  SSHUDOptionsLayer.m
//  SGF
//
//  Created by shanshan on 10-6-16.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SSOptionsLayer.h"
#import "GameSoundManager.h"
#import "SSGlobal.h"
#import "SSStorage.h"


@implementation SSOptionsLayer

- (id) initWithColor:(ccColor4B)color width:(GLfloat)w  height:(GLfloat) h{
	self = [super initWithColor:color width:w height:h];
	
	if(self != nil)
	{
		CGSize size = [[CCDirector sharedDirector] winSize];
		
		soundEngine = [[GameSoundManager sharedManager] soundEngine];
		//SSLogoLayer *logoLayer = [SSLogoLayer node];
		//        [self addChild:logoLayer];
		
		//[CCMenuItemFont setFontName:@"Arial"];
		[CCMenuItemFont setFontSize:20];
		
		int fontSize = 25;
		int smallFontSize = 18;
		
		//	CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:@"options_close.png" selectedImage:@"options_open.png" disabledImage:@"options_open.png" target:self selector:@selector(onNext:)];
		
		//	CCMenuItem *menuItem1 = [CCMenuItemImage :NSLocalizedString(@"Next", @"Next") target:self selector:@selector(onNext:)];
		CCMenuItemToggle *menuItemResume = [CCMenuItemFont itemFromString:NSLocalizedString(@"Resume", @"Resume") target:self selector:@selector(onResume:)];
		
		CCLabel *musicLabel = [[CCLabel alloc] initWithString:NSLocalizedString(@"Music", @"Music") fontName:@"Marker Felt" fontSize:smallFontSize];
		CCMenuItemToggle *menuItemMusic = [CCMenuItemToggle itemWithTarget:self selector:@selector(onMusic:) items:
													 [CCMenuItemImage itemFromNormalImage:@"options_close.png" selectedImage:@"options_close.png"],
													 [CCMenuItemImage itemFromNormalImage:@"options_open.png" selectedImage:@"options_open.png"], nil];
		if ([SSStorage getMusic]) {
			[menuItemMusic setSelectedIndex:1];
		}else {
			[menuItemMusic setSelectedIndex:0];
		}

		
		CCLabel *soundLabel = [[CCLabel alloc] initWithString:NSLocalizedString(@"Sound", @"Sound") fontName:@"Marker Felt" fontSize:smallFontSize];
		CCMenuItemToggle *menuItemSound = [CCMenuItemToggle itemWithTarget:self selector:@selector(onSound:) items:
									   [CCMenuItemImage itemFromNormalImage:@"options_close.png" selectedImage:@"options_close.png"],
									   [CCMenuItemImage itemFromNormalImage:@"options_open.png" selectedImage:@"options_open.png"], nil];
		if ([SSStorage getSound]) {
			[menuItemSound setSelectedIndex:1];
		}else {
			[menuItemSound setSelectedIndex:0];
		}
		
	//	CCMenuItem *menuItemExtra = [CCMenuItemFont itemFromString:NSLocalizedString(@"Extra", @"Extra") target:self selector:@selector(onExtra:)];
		CCMenuItem *menuItemExtra = [CCMenuItemFont itemFromString:NSLocalizedString(@"Extra", @"Extra") target:self selector:@selector(onExtra:)];
		
		
		CCMenu *menu = [CCMenu menuWithItems:menuItemResume, menuItemMusic, menuItemSound, menuItemExtra, nil];
		
	//	[menu alignItemsHorizontallyWithPadding:10];
		
		//menu.position = ccp(size.width/2, size.height/2);
		menu.position = CGPointZero;
		int distance = 55;
		int startY = 300;
		
	//	//itemBackgroundDefault.position = ccp(100, startY);
//		menuItemResume.position = ccp(100, startY);
//		
		musicLabel.position = ccp(150, startY - distance);
		menuItemMusic.position = ccp(310, startY - distance);
//		
		soundLabel.position = ccp(150, startY - distance*2);
		menuItemSound.position = ccp(310, startY - distance*2);
//		
		
		menuItemExtra.position = ccp(150, startY - distance*4);
		menuItemResume.position = ccp(310, startY - distance*4);
		
		[self addChild:menu];
		[self addChild:musicLabel z: 102];
		[self addChild:soundLabel z: 102];
		
	}
	return self;
}

-(void)onMusic:(id)sender{
	[[SSGlobal instance] clickButton];
	
	if ([sender selectedIndex] == 0) {
		[soundEngine pauseBackgroundMusic];
		[SSStorage setMusic:NO];
	}else {
		[soundEngine resumeBackgroundMusic];
		[SSStorage setMusic:YES];
	}
}

-(void)onSound:(id)sender{
	[[SSGlobal instance] clickButton];
	
	if ([sender selectedIndex] == 0) {
		[SSStorage setSound:NO];
	}else {
		[SSStorage setSound:YES];
	}
}

-(void)onExtra:(id)sender{
	[[SSGlobal instance] clickButton];
	
	CMLog(@"onSound");
}

-(void)onResume:(id)sender{
	[[SSGlobal instance] clickButton];
	
	CMLog(@"onResume");
	CMLog(@"optionsLayer: %@",[ [self parent] getChildByTag:'lJoy']);
	CMLog(@"optionsLayer: %@",[ [[self parent]parent] getChildByTag:'lJoy']);
	CMLog(@"optionsLayer: %@",[ [[[self parent]parent]parent] getChildByTag:'lJoy']);
	[[ [self parent] getChildByTag:'pbtn'] setVisible:YES];
	[[ [[[self parent]parent]parent] getChildByTag:'lJoy'] setVisible:YES];
	[[ [[[self parent]parent]parent] getChildByTag:'rBtn'] setVisible:YES];
	[[CCDirector sharedDirector] resume];
	//[self removeFromParentAndCleanup:YES];
	[self.parent removeChild:self cleanup:YES];
}

-(void)onMainMenu:(id)sender{
	CMLog(@"onMainMenu");
}

@end
